package com.william.common;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * CommonConfig class
 *
 * @author william
 * @date 2021/1/12
 */
@Configuration
public class CommonConfig {
    private Integer connectionTimeout=5*60*1000;
    private Integer readTimeout=5*60*1000;

    @Bean
    //@LoadBalanced
    public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        clientHttpRequestFactory.setConnectionRequestTimeout(connectionTimeout);
        clientHttpRequestFactory.setReadTimeout(readTimeout);
        RestTemplate template = new RestTemplate(clientHttpRequestFactory);

        List<HttpMessageConverter<?>> converters = template.getMessageConverters();
        converters.add(new ByteArrayHttpMessageConverter());

//        UTF8HttpMessageConverter utf8Converter = new UTF8HttpMessageConverter();
//        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
//        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
//        supportedMediaTypes.add(MediaType.TEXT_HTML);
//        utf8Converter.setSupportedMediaTypes(supportedMediaTypes);
//        converters.add(utf8Converter);
//
//        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
//        fastJsonConverter.setSupportedMediaTypes(supportedMediaTypes);
//        fastJsonConverter.setFeatures(JsonUtils.features);
//        converters.add(fastJsonConverter);

        template.setMessageConverters(converters);
        return template;
    }
}
